<?php
// +----------------------------------------------------------------------
// | Site:  [ http://www.yzmcms.com]
// +----------------------------------------------------------------------
// | Copyright: 袁志蒙工作室，并保留所有权利
// +----------------------------------------------------------------------
// | Author: YuanZhiMeng <214243830@qq.com>
// +---------------------------------------------------------------------- 
// | Explain: 这不是一个自由软件,您只能在不用于商业目的的前提下对程序代码进行修改和使用，不允许对程序代码以任何形式任何目的的再发布！
// +----------------------------------------------------------------------

defined('IN_YZMPHP') or exit('Access Denied'); 
yzm_base::load_controller('common', 'admin', 0);
yzm_base::load_sys_class('page','',0);
yzm_base::load_common('class/xlswriter.class.php');

class collect extends common {

	/**
	 * 配置信息
	 */
	
	public $config;
	public $data=[];
	public $field_map = [];
	public function __construct(){
		$this->field_map = get_setting();
		parent::__construct();
	}
	
	//货盘列表
	public function collect_list() {
		$d_list = D('collect_list');
		$page = input('get.page');
		$limit = input('get.limit');
		if($page){
			$total = $d_list->where('1=1')->total();
			$page = new page($total, $limit);
			$this->data = $d_list->alias('l')->field('l.*,c.m_name')->where('1=1')->join(C('db_prefix').'collect_category c on l.c_id = c.id','left')->limit($page->limit())->select();
			$this->_data_format();
			$data = ['code'=>0,'msg'=>'','count'=>$total,'data'=>$this->data];
			exit(json_encode($data));
		}
		include $this->admin_tpl('collect/collect_list');
	}
	
	//供应商列表
	public function collect_supplier(){
		$d_supplier = D('collect_supplier');
		$type = $_GET['type'] ?? '';
		if($type=='add'){
			if(is_post()){
				$result = $d_supplier->insert($_POST);
				if($result){
					return_json(array('status'=>1,'message'=>L('operation_success')));
				}else{
					return_json(array('status'=>0,'message'=>L('operation_failure')));
				}
			}
			include $this->admin_tpl('collect/collect_supplier_add');
			exit;
		}
		
		$page = input('get.page');
		$limit = input('get.limit');
		$of = input('get.field');
		$or = input('get.order');
		$supplier_name = trim(input('get.supplier_name'));
		$remark = trim(input('get.remark'));
		$code = trim(input('get.code'));
		$where = '1=1';
		if($remark){
			$where .=' AND remark like "%'.$remark.'%"';
		}
		if($code){
			$where .=' AND code like "%'.$code.'%"';
		}
		if($supplier_name){
			$where .=' AND supplier_name like "%'.$supplier_name.'%"';
		}
		$of = in_array($of, array('add_time')) ? $of : 'id';
		$or = in_array($or, array('asc','desc')) ? $or : 'desc';
		if($page){
			$total = $d_supplier->where($where)->total();
			$page = new page($total, $limit);
			$this->data = $d_supplier->where($where)->order("$of $or")->limit($page->limit())->select();
			$data = ['code'=>0,'msg'=>'','count'=>$total,'data'=>$this->data];
			exit(json_encode($data));
		}
		include $this->admin_tpl('collect/collect_supplier');
	}
	
	//店铺列表
	public function collect_shop(){
		$d_shop = D('collect_shop');
		$type = $_GET['type'] ?? '';
		//店铺平台
		$platform = get_setting('collect_platform');
		$platform_arr = [];
		foreach($platform as $k=>$v){
			$platform_arr[$k]['id'] = $k;
			$platform_arr[$k]['title'] = $v;
		}
		//店铺状态
		$shop_status = get_setting('collect_shop_status');
		$shop_status_arr = [];
		foreach($shop_status as $k=>$v){
			$shop_status_arr[$k]['id'] = $k;
			$shop_status_arr[$k]['title'] = $v;
		}
		//添加或者编辑
		if($type=='add'){
			if(is_post()){
				$_POST['other_info'] = json_encode($_POST['other']);
				$_POST['auth_expire'] = $_POST['auth_expire'] ? strtotime($_POST['auth_expire']) : 0;
				if($_POST['id']){
					$result = $d_shop->update($_POST,['id'=>$_POST['id']]);
				}else{
					$result = $d_shop->insert($_POST);
				}
				if($result!==false){
					return_json(array('status'=>1,'message'=>L('operation_success')));
				}else{
					return_json(array('status'=>0,'message'=>L('operation_failure')));
				}
			}
			if($_GET['id']){
				$data = $d_shop->where(['id'=>$_GET['id']])->find();
				$data['shop_status_text'] = $shop_status[$data['shop_status']];
				$data['platform_text'] = $platform[$data['platform']];
				$data['auth_expire'] = date('Y-m-d',$data['auth_expire']);
				$data['other'] = json_decode($data['other_info'],true);
			}
			include $this->admin_tpl('collect/collect_shop_add');
			exit;
		}
		
		$page = input('get.page');
		$limit = input('get.limit');
		$of = input('get.field');
		$or = input('get.order');
		$supplier_name = trim(input('get.shop_name'));
		$remark = trim(input('get.remark'));
		$code = trim(input('get.code'));
		$where = '1=1';
		if($remark){
			$where .=' AND remark like "%'.$remark.'%"';
		}
		if($code){
			$where .=' AND code like "%'.$code.'%"';
		}
		if($supplier_name){
			$where .=' AND supplier_name like "%'.$supplier_name.'%"';
		}
		$of = in_array($of, array('add_time')) ? $of : 'id';
		$or = in_array($or, array('asc','desc')) ? $or : 'desc';
		if($page){
			$total = $d_shop->where($where)->total();
			$page = new page($total, $limit);
			$this->data = $d_shop->where($where)->order("$of $or")->limit($page->limit())->select();
			$this->_data_format();
			//管理员列表
			foreach($this->data as &$v){
				$admins = explode(',',$v['adminids']);
				$admins_temp = [];
				foreach($admins as $val){
					$admin = get_admin($val);
					$admins_temp[] = $admin['adminname'];
				}
				$v['adminids'] = implode(',',$admins_temp);
			}
			$data = ['code'=>0,'msg'=>'','count'=>$total,'data'=>$this->data];
			exit(json_encode($data));
		}
		include $this->admin_tpl('collect/collect_shop');
	}
	
	public function get_admin(){
		$admin_list = get_admin();
		$data = [];
		foreach($admin_list as $v){
			$data[] = $v;
		}
		exit(json_encode($data));
	}
	
	private function _data_format(){
		if(!$this->data){
			return [];
		}
		foreach($this->data as $k=>&$v){
			foreach($v as $key=>&$val){
				if($this->field_map['collect_'.$key]){
					$val = $this->field_map['collect_'.$key][$val];
				}
			}
		}
	}
}